package org.estela.util;

/**
 * Class used to test code invariants.
 * 
 * @since 1.00
 * @author Hikaru Kurosaki
 * @version $Revision$
 */
public class Assert {

	public static Object assertNotNull(Object obj, String message) {
		if (obj == null) {
			throw new NullPointerException("Assertion failed: " + message);
		}
		return obj;
	}
	
	public static void assertTrue(boolean cond, String message) {
		if (!cond) {
			throw new IllegalArgumentException("Assertion failed: " + message);
		}
	}

	public static Object assertInstanceOf(Object obj, Class clazz, String message) {
		if (!clazz.isAssignableFrom(obj.getClass())) {
			throw new IllegalArgumentException("Assertion failed: " + message);
		}
		return obj;
	}

	public static void assertFalse(boolean cond, String message) {
		if (cond) {
			throw new IllegalArgumentException("Assertion failed: " + message);
		}
	}

	public static void assertTrue(boolean cond) {
		assert cond;
	}

	public static void assertNotEmpty(Object[] arr, String msg) {
		if (arr.length == 0) {
			throw new IllegalArgumentException(msg);
		}
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] == null) {
				throw new NullPointerException(msg + " [position " + i + " is null]");
			}
		}
		
	}
}
